<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改活动课内容')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: summernote-css" />
    <th:block th:include="include :: bootstrap-fileinput-css" />
    <link th:href="@{/css/simplemde.min.css}" rel="stylesheet"/>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-courseContent-edit" th:object="${courseContent}">
            <input name="id" th:field="*{id}" type="hidden">

            <div class="form-group">
                <label class="col-sm-3 control-label">课程ID：</label>
                <div class="col-sm-8">
                    <input name="courseId" class="form-control" type="text" th:field="*{courseId}" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">显示顺序：</label>
                <div class="col-sm-8">
                    <input name="orderNum" th:field="*{orderNum}" class="form-control" type="text">
                </div>
            </div>
<!--            <div class="form-group">-->
<!--                <label class="col-sm-3 control-label">时长：</label>-->
<!--                <div class="col-sm-8">-->
<!--                    <input name="durations"  th:field="*{durations}" class="form-control" type="text">-->
<!--                </div>-->
<!--            </div>-->
            <div class="form-group" id="beginAt_div">
                <label class="col-sm-3 control-label">开始时间：</label>
                <div class="col-sm-8">
                    <input id="beginAt" name="beginAt"  th:field="*{beginAt}"  class="form-control" type="text">
                </div>
            </div>
            <div class="form-group" id="endAt_div">
                <label class="col-sm-3 control-label">结束时间：</label>
                <div class="col-sm-8">
                    <input id="endAt" name="endAt"  th:field="*{endAt}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">菜单样式：</label>
                <div class="col-sm-8">
                    <select id="menuStyle" name="menuStyle"  th:field="*{menuStyle}"class="form-control m-b" th:with="type=${@dict.getType('menuStyle')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">子集类型：</label>
                <div class="col-sm-8">
                    <select name="parentType" th:field="*{parentType}" class="form-control m-b"  th:with="type=${@dict.getType('parentType')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">类型：</label>
                <div class="col-sm-8">
                    <select name="categoryType"  th:field="*{categoryType}"class="form-control m-b"  th:with="type=${@dict.getType('course_categoryType')}">
                        <option value="0">本课程</option>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">标题：</label>
                <div class="col-sm-8">
                    <input name="title" th:field="*{title}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label"></label>
                <div class="col-sm-8">
                    <input id="multipleFile" name="files" type="file" multiple>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">内容：</label>
                <div class="col-sm-8">
                    <input type="hidden" class="form-control" th:field="*{content}" id="content" name="content">
                    <textarea></textarea>
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">注意事项：</label>
                <div class="col-sm-8">
                    <input name="remark" th:field="*{remark}" class="form-control" type="text">
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: summernote-js" />
    <th:block th:include="include :: bootstrap-fileinput-js" />
    <script th:src="@{/js/simplemde.min.js}"></script>
    <script th:inline="javascript">
        var simplemde = new SimpleMDE();
        var prefix = ctx + "system/courseContent";
        $("#form-courseContent-edit").validate({
            focusCleanup: true
        });
        setTimeout(function () {
            var content = $("#content").val();
            simplemde.value(content);
        }, 1500);
        function submitHandler() {
            if ($.validate.form()) {
                var testPlain = simplemde.value();
                $("#content").val(testPlain);
                $.operate.save(prefix + "/edit", $('#form-courseContent-edit').serialize());
            }
        }


        $("#multipleFile").fileinput({
            uploadUrl: '/system/courseContent/importImgList',
            uploadAsync: false
        }).on('filebatchuploadsuccess', function (event, data, previewId, index) {
            var rsp = data.response;
            var testPlain = simplemde.value();
            var urls  = rsp.urls.split(",");
            var url = "";
            for (let i = 0; i < urls.length; i++) {
                url = url + "![]("+urls[i]+")";
            }
            // simplemde.value(testPlain+url);
            $.modal.alertWarning(url);
            log.info("return urls：" + rsp.urls)
        }).on('fileremoved', function (event, id, index) {
            $("input[name='" + event.currentTarget.id + "']").val('')
        })

        $(function() {
            $('.summernote').summernote({
                lang: 'zh-CN',
                dialogsInBody: true,
                callbacks: {
                    onChange: function(contents, $edittable) {
                        $("input[name='" + this.id + "']").val(contents);
                    },
                    onImageUpload: function(files) {
                        var obj = this;
                        var data = new FormData();
                        data.append("file", files[0]);
                        $.ajax({
                            type: "post",
                            url: ctx + "common/upload",
                            data: data,
                            cache: false,
                            contentType: false,
                            processData: false,
                            dataType: 'json',
                            success: function(result) {
                                if (result.code == web_status.SUCCESS) {
                                    $('#' + obj.id).summernote('insertImage', result.url);
                                } else {
                                    $.modal.alertError(result.msg);
                                }
                            },
                            error: function(error) {
                                $.modal.alertWarning("图片上传失败。");
                            }
                        });
                    }
                }
            });
            getMenuStyle();
        });




        function getMenuStyle() {
            var endAt = $("#endAt").val();
            //机构端
            if (endAt.length > 1) {
                $("#endAt_div").attr("style", "display:block;");//显示div
                $("#beginAt_div").attr("style", "display:block;");//显示div
            }else {
                $("#endAt_div").attr("style", "display:none;");//隐藏div
                $("#beginAt_div").attr("style", "display:none;");//隐藏div
                $("#endAt").val("");//给空值
                $("#beginAt").val("");//给空值
            }
        }
    </script>
</body>
</html>